package cn.stylefeng.guns.modular.ercomment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.ercomment.entity.ErCommentLove;
import cn.stylefeng.guns.modular.ercomment.model.result.Commenterinfo;
import cn.stylefeng.guns.modular.ercomment.entity.ErComment;
import cn.stylefeng.guns.modular.ercomment.mapper.ErCommentMapper;
import cn.stylefeng.guns.modular.ercomment.model.param.ErCommentParam;
import cn.stylefeng.guns.modular.ercomment.service.ErCommentService;
import cn.stylefeng.guns.modular.ercomment.enums.ErCommentExceptionEnum;
import cn.stylefeng.guns.modular.erlist.model.param.ErListParam;
import cn.stylefeng.guns.modular.erlove.entity.ErLove;
import cn.stylefeng.guns.modular.erlove.model.param.ErLoveParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 评论 服务实现类
 *
 * @author wpk
 * @date 2021/06/25 15:19
 */
@Service
public class ErCommentServiceImpl extends ServiceImpl<ErCommentMapper, ErComment> implements ErCommentService {

    @Override
    public PageResult<ErComment> pageComment(ErCommentParam erCommentParam) {
        System.out.println(erCommentParam.getEid());
        // 根据帖子id获取指定评论列表
        LambdaQueryWrapper<ErComment> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(erCommentParam)){
            if(ObjectUtil.isNotNull(erCommentParam.getEid())){
                queryWrapper.and(q->q.eq(ErComment::getEid,erCommentParam.getEid()));
            }
        }
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<ErComment> list(ErCommentParam erCommentParam) {

        // 构造条件
        LambdaQueryWrapper<ErComment> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public ErComment add(ErCommentParam erCommentParam) {
        // 将dto转为实体
        ErComment erComment = new ErComment();
        BeanUtil.copyProperties(erCommentParam, erComment);
        this.save(erComment);
        Commenterinfo commenterinfo = this.baseMapper.getCommenter(erComment.getCreateUser());
        System.out.println(commenterinfo);
        erComment.setAvatar(commenterinfo.getAvatar());
        erComment.setNickName(commenterinfo.getNickName());
        this.updateById(erComment);
        System.out.println(erComment);
        return erComment;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ErCommentParam erCommentParam) {
        this.removeById(erCommentParam.getId());
    }

    @Override
    public void edit(ErCommentParam erCommentParam) {

        // 根据id查询实体
        ErComment erComment = this.queryErComment(erCommentParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(erCommentParam, erComment);

        this.updateById(erComment);
    }

    @Override
    public int love(ErCommentParam erCommentParam) {
        // 将dto转为实体
        // 1:  点赞成功   0：取消点赞
        int state = 0;
        if(ObjectUtil.isNotEmpty(erCommentParam)){
            state = this.baseMapper.showlikestate(erCommentParam.getId(),erCommentParam.getCreateUser());
        }
        if(state == 0 )
        {
            this.baseMapper.addlike(erCommentParam.getId(),erCommentParam.getCreateUser());
            state = 1;

        }else{
            this.baseMapper.offlike(erCommentParam.getId(),erCommentParam.getCreateUser());
            state = 0;
        }
        return state;
    }

    @Override
    public int showLoveState(ErCommentParam erCommentParam) {
        // 将dto转为实体
       // 1:  已点赞   0：未点赞
        int state = 0;
        if(ObjectUtil.isNotEmpty(erCommentParam)){
            state = this.baseMapper.showlikestate(erCommentParam.getId(),erCommentParam.getCreateUser());
        }
        if( state == 0 )
        {
            state = 0;
        }else{
            state = 1;
        }
        return state;
    }

    @Override
    public ErComment detail(ErCommentParam erCommentParam) {
        return this.queryErComment(erCommentParam);
    }



    /**
     * 获取评论
     *
     * @author wpk
     * @since 2021/06/25 15:19
     */
    private ErComment queryErComment(ErCommentParam erCommentParam) {
        ErComment erComment = this.getById(erCommentParam.getId());
        if (ObjectUtil.isEmpty(erComment)) {
            throw new ServiceException(ErCommentExceptionEnum.NOT_EXISTED);
        }
        return erComment;
    }
}